package com.example.spsaveqqty.attraction;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.spsaveqqty.R;

import java.util.List;

public class CommentActivity extends AppCompatActivity {

    private RatingBar ratingBar;
    private TextView tv_attracName;
    private Button submit;
    private EditText et_content;
    private float star;
    private String content;
    private Comment comment;
    private String attract_name;
    private String username;
    private AttractionDao dao;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);

        Intent i = getIntent();
        username = CheckUser.isLogin();
        attract_name = i.getStringExtra("attract_name");


        ratingBar = findViewById(R.id.rb_star);
        tv_attracName = findViewById(R.id.tv_attracName);
        submit = findViewById(R.id.submit);
        et_content = findViewById(R.id.content);

        tv_attracName.setText(attract_name);

        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                star = rating;
//                Toast.makeText(CommentActivity.this,rating+"星" ,Toast.LENGTH_SHORT).show();

            }
        });

        et_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                content =s.toString().trim();
            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(star == 0){
                    Toast.makeText(CommentActivity.this,"还没有星星哦~" ,Toast.LENGTH_SHORT).show();
                }else{
                    comment = new Comment();
                    comment.setStar((double)star);
                    comment.setAttrac_name(attract_name);
                    comment.setContent(content);
                    comment.setUsername(username);

                    insertComment(comment);
                    Toast.makeText(CommentActivity.this,"评论成功！" ,Toast.LENGTH_SHORT).show();
                    CommentActivity.this.finish();
                    toInfo(comment.getAttrac_name());

                }
            }
        });



    }

    public void toInfo(String attracName){
        dao = new AttractionDao(CommentActivity.this);
        List<Attraction> attracList = null;
        attracList = dao.select(attracName);

        Intent intent = new Intent(CommentActivity.this, InformationActivity.class);
        intent.putExtra("attraction", attracList.get(0));
        startActivity(intent);
    }

    public void insertComment(Comment comment){
//        Log.i("--------daobf---------", comment.toString());

        dao = new AttractionDao(CommentActivity.this);

        dao.insertComment(comment);
//        Log.i("-------daofffff----", "wwwwwwwwwwwwww");
        //修改评分
        dao.uodateScore(comment.getAttrac_name());
    }


}
